<?php

/* rdo */
header("Content-type: text/html; charset=utf-8;");
require '../ppf/fun.php';
require '../ppf/pdo_mysql.php';

session_start();
if (empty($_SESSION['uid'])) {
    $tips = array(
        'tips' => '请登录后再进行操作',
        'url' => 'index.php'
    );
    $tips = urlencode(serialize($tips));
    header('Location:/tips.php?gets=' . $tips);
    exit;
}
$pd = new pdo_mysql();

switch ($_POST["tpl"]) {
    /*管理员添加此条报修备注*/
    case "btn_takes_code_admin":
        $sql = "update sys_takes_list set status = '".$_POST['status_val']."',remarks='".$_POST['takes_code']."' where id = ".$_POST['id'];
        $pd->exec($sql);
        echo "ok";
        break;

    /*维修人员修改维修状态*/
    case "edit_status":
        $sql = "update sys_takes_list set status = '".$_POST['takes_status']."' where id = ".$_POST['edit_id'];
        $pd->exec($sql);
        echo "ok";
        break;
    /*添加备注*/
    case "btn_takes_code":
        $onesql = "select * from sys_takes_codes where takes_id = ".$_POST['id']." and takes_user_id='".$_SESSION['uid']."' and takes_stauts=".$_POST['status_val'];
        $is_one = $pd->query($onesql)->fetch(PDO::FETCH_ASSOC);
        /*添加备注信息*/

        if($is_one == ""){
            $sql = "insert into sys_takes_codes (takes_id,takes_user_id,takes_stauts,takes_code) VALUES('{$_POST['id']}','{$_SESSION['uid']}','{$_POST['status_val']}','{$_POST['takes_code']}')";
            $pd->exec($sql);
        }else{
            $sql = "update sys_takes_codes set takes_code='".$_POST['takes_code']."' where takes_id = ".$_POST['id']." and takes_user_id='".$_SESSION['uid']."' and takes_stauts=".$_POST['status_val'];
            $pd->exec($sql);
        }
        $sql = "update sys_takes_list set status = '".$_POST['status_val']."' where id = ".$_POST['id'];
        $pd->exec($sql);
        echo "ok";
        break;
    /*接受维修任务*/
    case "accept_mytakes":
        $sql = "update sys_takes_list set status = 8 where id = ".$_POST['id'];
        $pd->exec($sql);
        echo "ok";
        break;
    /*追加评价报修申请*/
    /* 追加评价报修申请 */
    case "btn_add_evaluate":
        $sql = "update sys_takes_list set status = 6,add_evaluate='" . $_POST['evaluate'] . "' where id = " . $_POST['id'];
        $pd->exec($sql);
        echo "ok";
        break;
    /* 评价报修申请 */
    case "btn_evaluate":
        $sql = "update sys_takes_list set status = 6,evaluate='" . $_POST['evaluate'] . "' where id = " . $_POST['id'];
        $pd->exec($sql);
        echo "ok";
        break;
    /* 撤回报修申请 */
    case "repair_callback":
        $sql = "update sys_takes_list set status = 7 where id = " . $_POST['id'] . " and release_user_id='" . $_SESSION['uid'] . "'";
        $pd->exec($sql);
        echo "ok";
        break;
    /* 删除我的报修 */
    case "del_mytakes":
        $sql = "update sys_takes_list set is_delete_false = 1 where id =" . $_POST['id'] . " and release_user_id='" . $_SESSION['uid'] . "'";
        //print_r($sql);exit;
        $pd->exec($sql);
        echo "ok";
        break;
    case "del_takes_lists":
        $pd->exec("delete from sys_takes_list where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    case "del_takes_list":
        $pd->exec("delete from sys_takes_list where id=" . $_POST['id']);
        echo "ok";
        break;
    case "del_repair_type":
        $pd->exec("delete from sys_equipment_repair where id=" . $_POST['id']);
        echo "ok";
        break;
    case "del_repair_typelist":
        $pd->exec("delete from sys_equipment_repair where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    case "del_dict_single":
        $pd->exec("delete from sys_dict where id=" . $_POST['id']);
        echo "ok";
        break;
    case "del_dict_more":
        $pd->exec("delete from sys_dict where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    case "del_role_single":
        $pd->exec("delete from sys_role_menu where rid=" . $_POST['id']);
        $pd->exec("delete from sys_role where id=" . $_POST['id']);
        echo "ok";
        break;
    case "del_role_more":
        $pd->exec("delete from sys_role_menu where rid in (" . $_POST['id'] . ")");
        $pd->exec("delete from sys_role where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    case "del_grp_single":
        //$pd->exec("delete from sys_role_menu where rid=".$_POST['id']);
        $pd->exec("delete from sys_custom_group where id=" . $_POST['id']);
        echo "ok";
        break;
    case "del_grp_more":
        //$pd->exec("delete from sys_role_menu where rid in (".$_POST['id'].")");
        $pd->exec("delete from sys_custom_group where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    case "del_grade_single":
        $pd->exec("delete from sys_grade where id=" . $_POST['id']);
        echo "ok";
        break;
    case "del_grade_more":
        $pd->exec("delete from sys_grade where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    case "del_dept_single":
        $pd->exec("delete from sys_department where id=" . $_POST['id']);
        echo "ok";
        break;
    case "del_dept_more":
        $pd->exec("delete from sys_department where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    case "del_school_single":
        $pd->exec("delete from sys_school where id=" . $_POST['id']);
        echo "ok";
        break;
    case "del_school_more":
        $pd->exec("delete from sys_school where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    case "check_school":
        $pd->exec("update sys_school set ischeck=(1-ischeck) where id =" . $_POST['id']);
        echo "ok";
        break;
    case "del_user_single":
        $pd->exec("delete from sys_member where id='" . $_POST['id'] . "'");
        echo "ok";
        break;
    case "del_user_more":
        $pd->exec("delete from sys_member where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    case "pass_user_single":
        $p = md5('12345');
        $pd->exec("update sys_member set pmd5='" . $p . "' where id='" . $_POST['id'] . "'");
        echo "ok";
        break;
    case "check_user_more":
        $pd->exec("update sys_member set ischeck=1 where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    #同时删除一条或多条消息
    case "del_news_more":
        $pd->exec("delete from sys_news where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    #删除一条消息
    case "del_news_single":
        $pd->exec("delete from sys_news where id=" . $_POST['id']);
        echo "ok";
        break;
    #标记已读未读	
    case "check_read_more":
        $pd->exec("update sys_news set is_read=1 where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    case "add_role_menu":
        $id = $_POST['id'];
        $mid = $_POST['mid'];
        $e = $pd->fetchOneNo(array('field' => 'count(*)', 'table' => 'sys_role_menu', 'where' => "rid=" . $id . ""));
        if ($e != 0) {
            if ($e == 1) {
                $pd->exec("update sys_role_menu set menu_id='" . $mid . "' where rid=" . $id);
            } else {
                $pd->exec("delete from sys_role_menu where rid=" . $id);
                $pd->exec("insert into sys_role_menu (rid,menu_id) values (" . $id . ",'" . $mid . "')");
            }
        } else {
            $pd->exec("insert into sys_role_menu (rid,menu_id) values (" . $id . ",'" . $mid . "')");
        }
        echo "ok";
        break;
    case "add_user_togrp":
        $id = $_POST['id'];
        $mid = $_POST['mid'];
        $e = $pd->fetchOneNo(array('field' => 'count(*)', 'table' => 'sys_user_togrp', 'where' => "gid=" . $id . ""));
        if ($e != 0) {
            if ($e == 1) {
                $pd->exec("update sys_user_togrp set user_id='" . $mid . "' where gid=" . $id);
            } else {
                $pd->exec("delete from sys_user_togrp where gid=" . $id);
                $pd->exec("insert into sys_user_togrp (gid,user_id) values (" . $id . ",'" . $mid . "')");
            }
        } else {
            $pd->exec("insert into sys_user_togrp (gid,user_id) values (" . $id . ",'" . $mid . "')");
        }
        echo "ok";
        break;
    #删除一条登录日志记录
    case "del_log_single":
        $pd->exec("delete from sys_log where id=" . $_POST['id']);
        echo "ok";
        break;
    #同时删除一条或多条登录日志
    case "del_log_more":
        $pd->exec("delete from sys_log where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    case "del_email_single":
        $pd->exec("update sys_email set delete_flag=1 where id='" . $_POST['id'] . "'");
        echo "ok";
        break;
    case "del_email_more":
        $pd->exec("update sys_email set delete_flag=1 where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    case "email_read":
        $pd->exec("update sys_email set read_flag=1 where id='" . $_POST['id'] . "'");
        echo "ok";
        break;
    case "cdel_email_single":
        $pd->exec("delete from sys_email where id='" . $_POST['id'] . "'");
        echo "ok";
        break;
    case "cdel_email_more":
        $pd->exec("delete from sys_email where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    case "renew_email_single":
        $pd->exec("update sys_email set delete_flag=0 where id='" . $_POST['id'] . "'");
        echo "ok";
        break;
    case "renew_email_more":
        $pd->exec("update sys_email set delete_flag=0 where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    #同时删除一条或多条通知
    case "del_notice_more":
        $type = isset($_POST["type"]) ? $_POST["type"] : "send";
        if ($type == 'send') {
            $pd->exec("delete from sys_notice where id in (" . $_POST['id'] . ")");
        } else {
            $pd->exec("delete from sys_notice_list where id in (" . $_POST['id'] . ")");
        }
        echo "ok";
        break;
    #删除一条通知
    case "del_notice_single":
        $type = isset($_POST["type"]) ? $_POST["type"] : "send";
        if ($type == 'send') {
            $pd->exec("delete from sys_notice where id=" . $_POST['id']);
        } else {
            $pd->exec("delete from sys_notice_list where id=" . $_POST['id']);
        }
        echo "ok";
        break;
    #同时删除一条或多条公告
    case "del_announce_more":
        $atts = $pd->query("select attachment from sys_announce where id in (" . $_POST['id'] . ")")->fetchall(PDO::FETCH_ASSOC);
        foreach ($atts as $k => $val) {
            $attarr = explode(',', $val['attachment']);
            foreach ($attarr as $key => $value) {
                $pd->exec("delete from sys_announce_attachment where id='" . $value . "'");
            }
        }
        $pd->exec("delete from sys_announce where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    #删除一条公告
    case "del_announce_single":
        $atts = $pd->query("select attachment from sys_announce where id=" . $_POST['id'])->fetchColumn(0);
        $attarr = explode(',', $atts);
        foreach ($attarr as $k => $val) {
            $pd->exec("delete from sys_announce_attachment where id='" . $val . "'");
        }
        $pd->exec("delete from sys_announce where id=" . $_POST['id']);
        echo "ok";
        break;
    #通知阅读状态
    case "notice_reader":
        $nid = $pd->query("select nid from sys_notice_list where id=" . $_POST['id'])->fetchColumn(0);
        $reader = $pd->query("select reader from sys_notice where id=" . $nid)->fetchColumn(0);
        if ($reader) {
            $readerarr = explode(',', $reader);
        } else {
            $readerarr = array();
        }
        if (!in_array($_SESSION['uid'], $readerarr)) {
            array_push($readerarr, $_SESSION['uid']);
        }
        $newreader = implode(',', $readerarr);
        $pd->exec("update sys_notice set reader='" . $newreader . "' where id=" . $nid);
        echo "ok";
        break;
    #同时删除一条或多条工作日志
    case "del_worklogs_more":
        $pd->exec("delete from sys_worklogs where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    #删除一条工作日志
    case "del_worklogs_single":
        $pd->exec("delete from sys_worklogs where id=" . $_POST['id']);
        echo "ok";
        break;
    #同时删除一条或多条工作日志类型
    case "del_worklogs_type_more":
        $pd->exec("delete from sys_worklogs_type where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    #删除一条工作日志类型
    case "del_worklogs_type_single":
        $pd->exec("delete from sys_worklogs_type where id=" . $_POST['id']);
        echo "ok";
        break;
    #同时删除一条或多条工作计划
    case "del_plan_more":
        $atts = $pd->query("select attachment from sys_plan where id in (" . $_POST['id'] . ")")->fetchall(PDO::FETCH_ASSOC);
        foreach ($atts as $k => $val) {
            $attarr = explode(',', $val['attachment']);
            foreach ($attarr as $key => $value) {
                $pd->exec("delete from sys_plan_attachment where id='" . $value . "'");
            }
        }
        $pd->exec("delete from sys_plan where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    #删除一条工作计划
    case "del_plan_single":
        $atts = $pd->query("select attachment from sys_plan where id=" . $_POST['id'])->fetchColumn(0);
        $attarr = explode(',', $atts);
        foreach ($attarr as $k => $val) {
            $pd->exec("delete from sys_plan_attachment where id='" . $val . "'");
        }
        $pd->exec("delete from sys_plan where id=" . $_POST['id']);

        echo "ok";
        break;
    #同时删除一条或多条工作计划类型
    case "del_plan_type_more":
        $pd->exec("delete from sys_plan_type where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    #删除一条工作计划类型
    case "del_plan_type_single":
        $pd->exec("delete from sys_plan_type where id=" . $_POST['id']);
        echo "ok";
        break;
    #手动设置一条工作计划状态
    case "set_plan":
        #setid:1立即开始，2立即结束，3恢复计划
        $id = $_POST['id'];
        $setid = $_POST['setid'];
        if ($setid == 1) {
            $pd->exec("update sys_plan set start_time='" . date('Y-m-d', time()) . "' where id=" . $id);
        } elseif ($setid == 2) {
            $pd->exec("update sys_plan set end_time='" . date('Y-m-d', time() - 86400) . "' where id=" . $id);
        } elseif ($setid == 3) {
            $pd->exec("update sys_plan set end_time='' where id=" . $id);
        }
        echo "ok";
        break;
    #同时删除一条或多条日程事务
    case "del_schedule_more":
        $pd->exec("delete from sys_schedule where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    #删除一条日程事务
    case "del_schedule_single":
        $pd->exec("delete from sys_schedule where id=" . $_POST['id']);
        echo "ok";
        break;
    #同时删除一条或多条日程事务类型
    case "del_schedule_type_more":
        $pd->exec("delete from sys_schedule_type where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    #删除一条日程事务类型
    case "del_schedule_type_single":
        $pd->exec("delete from sys_schedule_type where id=" . $_POST['id']);
        echo "ok";
        break;
    #同时删除一条或多条作息表
    case "del_rest_more":
        $pd->exec("delete from sys_rest where id in (" . $_POST['id'] . ")");
        echo "ok";
        break;
    #删除一条作息表
    case "del_rest_single":
        $pd->exec("delete from sys_rest where id=" . $_POST['id']);
        echo "ok";
        break;
    #手动设置一条作息表启用
    case "set_rest":
        $id = $_POST['id'];
        $pd->exec("update sys_rest set display=0");
        $pd->exec("update sys_rest set display=1 where id=" . $id);
        echo "ok";
        break;
    #设置免签人员
    case "edit_attend_free":
        $uids = $_POST['uids'];
        $isset = $pd->query("select count(1) from sys_attend_free")->fetchColumn(0);
        if ($isset) {
            $pd->exec("update sys_attend_free set uids='" . $uids . "' where id=1");
        } else {
            $pd->exec("insert into sys_attend_free(id,uids) values(1,'" . $uids . "')");
        }
        echo "ok";
        break;
    #设置打卡时间段
    case "edit_attend_time":
        $isset = $pd->query("select count(1) from sys_attend_time")->fetchColumn(0);
        if ($isset) {
            $pd->exec("update sys_attend_time set 
                                                on_before='" . $_POST['on_before'] . "',
                                                on_after='" . $_POST['on_after'] . "',
                                                off_before='" . $_POST['off_before'] . "',
                                                off_after='" . $_POST['off_after'] . "',
                                                time='" . date('Y-m-d H:i:s') . "'
             where id=1");
        } else {
            $pd->exec("insert into sys_attend_time(id,on_before,on_after,off_before,off_after,time) 
                            values(
                                    1,
                                    '" . $_POST['on_before'] . "',
                                    '" . $_POST['on_after'] . "',
                                    '" . $_POST['off_before'] . "',
                                    '" . $_POST['off_after'] . "',
                                    '" . date('Y-m-d H:i:s') . "'
            )");
        }
        echo "ok";
        break;
    //删除文件及目录
    case "del_file";
        $arr = explode("/", $_POST['file_path']);
        array_pop($arr);
        $dir = implode("/", $arr);
        unlink($_SERVER['DOCUMENT_ROOT'] . $_POST['file_path']);    //删除文件
        if (count(scandir($_SERVER['DOCUMENT_ROOT'] . $dir)) == 2) { //如果是空的目录    
            rmdir($_SERVER['DOCUMENT_ROOT'] . $dir); //直接删除    
        }

        echo json_encode(array("tips" => "删除成功", "status" => true));
        break;
    //删除文件发送列表记录
    case "del_file_upload";
        $id = rtrim($_POST['id'], ",");
        $res = $pd->exec("update file_upload set senddele=1 where id in({$id})");
        if ($res === false) {
            echo json_encode(array("tips" => "删除失败", "status" => false));
        } else {
            echo json_encode(array("tips" => "删除成功", "status" => true));
        }

        break;
    //删除签收文件列表记录
    case "del_file_upload_receive";
        $id = rtrim($_POST['id'], ",");
        $res = $pd->exec("update file_upload set recvdele=concat_ws('','" . $_SESSION['username'] . ",',recvdele) where id in({$id})");
        if ($res === false) {
            echo json_encode(array("tips" => "删除失败", "status" => false));
        } else {
            echo json_encode(array("tips" => "删除成功", "status" => true));
        }

        break;
    //签收签收文件列表记录
    case "file_upload_readers";
        $id = rtrim($_POST['id'], ",");
        $res = $pd->exec("update file_upload set readers=concat_ws('','" . $_SESSION['username'] . ",',readers) where id in({$id})");
        if ($res === false) {
            echo json_encode(array("tips" => "签收失败", "status" => false));
        } else {
            echo json_encode(array("tips" => "签收成功", "status" => true));
        }

        break;
}
$pd->close();
unset($pd);
